package cn.zmwh.manage.admin.modules.upms.service;

import cn.zmwh.common.core.constants.Constants;
import cn.zmwh.manage.admin.modules.upms.mapper.SysMenuMapper;
import cn.zmwh.manage.admin.modules.upms.mapper.SysRoleMapper;
import cn.zmwh.manage.admin.modules.upms.pojo.entity.SysRole;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色管理 服务实现类
 * </p>
 *
 * @author dmzmwh
 * @since 2025-03-11
 */
@Service
public class SysRoleService extends ServiceImpl<SysRoleMapper, SysRole> {

    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    public void removeRoleById(Integer id) {
        baseMapper.deleteById(id);
        sysRoleMenuService.delByRoleId(id);
        sysUserRoleService.delByRoleId(id);
    }

    public Set<String> listByUser(Long uid, String username) {
        Set<String> role;
        if (Constants.LTY.equals(username)) {
            List<SysRole> sysRoles = this.list();
            role = sysRoles.stream().map(SysRole::getRole).collect(Collectors.toSet());
        } else {
            role = sysMenuMapper.findByUid(uid);
        }
        return role;
    }

}
